package com.aries.ui.view.radius.delegate;


import com.aries.ui.util.AttrUtils;
import com.aries.ui.util.PixelUtil;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Switch;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.StateElement;
import ohos.agp.utils.Color;
import ohos.app.Context;


/**
 * @Author: AriesHoo on 2018/7/19 9:58
 * @E-Mail: AriesHoo@126.com
 * Function: 设置Switch thumb及track相关代理
 * Description:
 * 2018-6-1 09:40:27 1、增加默认属性值并调整设置Drawable逻辑
 */
public class RadiusSwitchDelegate extends RadiusCompoundDelegate<RadiusSwitchDelegate> {

    private Switch mSwitch;
    private StateElement mStateThumbDrawable;
    private StateElement mStateTrackDrawable;

    /**
     * 以下为xml对应属性解析
     */
    private int mThumbDrawableWidth;
    private int mThumbDrawableHeight;
    private Element mThumbDrawable;
    private Element mThumbPressedDrawable;
    private Element mThumbDisabledDrawable;
    private Element mThumbSelectedDrawable;
    private Element mThumbCheckedDrawable;

    private int mThumbStrokeColor;
    private int mThumbStrokePressedColor;
    private int mThumbStrokeDisabledColor;
    private int mThumbStrokeSelectedColor;
    private int mThumbStrokeCheckedColor;
    private int mThumbStrokeWidth;
    private int mThumbRadius;

    private int mTrackDrawableWidth;
    private int mTrackDrawableHeight;
    private Element mTrackDrawable;
    private Element mTrackPressedDrawable;
    private Element mTrackDisabledDrawable;
    private Element mTrackSelectedDrawable;
    private Element mTrackCheckedDrawable;

    private int mTrackStrokeColor;
    private int mTrackStrokePressedColor;
    private int mTrackStrokeDisabledColor;
    private int mTrackStrokeSelectedColor;
    private int mTrackStrokeCheckedColor;
    private int mTrackStrokeWidth;
    private int mTrackRadius;
    private int mColorAccent;
    private int mColorDefault;

    //以上为xml对应属性解析

    public RadiusSwitchDelegate(Switch view, Context context, AttrSet attrs) {
        super(view, context, attrs);
    }

    @Override
    protected void initAttributes(Context context, AttrSet attrs) {
        mColorAccent = Color.getIntColor("#393939");
        mColorDefault = Color.LTGRAY.getValue();
        mThumbDrawableWidth = AttrUtils.getDimensionFromAttr(attrs,"rv_thumbDrawableWidth", (int) PixelUtil.fp2px(24));
        mThumbDrawableHeight = AttrUtils.getDimensionFromAttr(attrs,"rv_thumbDrawableHeight", (int)PixelUtil.fp2px(24));
        mThumbDrawable = AttrUtils.getElementFromAttr(attrs,"rv_thumbDrawable",null);
        mThumbPressedDrawable =  AttrUtils.getElementFromAttr(attrs,"rv_thumbPressedDrawable",null);
        mThumbDisabledDrawable = AttrUtils.getElementFromAttr(attrs,"rv_thumbDisabledDrawable",null);
        mThumbSelectedDrawable = AttrUtils.getElementFromAttr(attrs,"rv_thumbSelectedDrawable",null);
        mThumbCheckedDrawable = AttrUtils.getElementFromAttr(attrs,"rv_thumbCheckedDrawable",null);
        mThumbStrokeColor = AttrUtils.getColorFromAttr(attrs,"rv_thumbStrokeColor", mColorDefault);
        mThumbStrokePressedColor =AttrUtils.getColorFromAttr(attrs,"rv_thumbStrokePressedColor", mThumbStrokeColor);
        mThumbStrokeDisabledColor = AttrUtils.getColorFromAttr(attrs,"rv_thumbStrokeDisabledColor", mThumbStrokeColor);
        mThumbStrokeSelectedColor = AttrUtils.getColorFromAttr(attrs,"rv_thumbStrokeSelectedColor", mThumbStrokeColor);
        mThumbStrokeCheckedColor = AttrUtils.getColorFromAttr(attrs,"rv_thumbStrokeCheckedColor", mColorAccent);
        mThumbStrokeWidth = AttrUtils.getDimensionFromAttr(attrs,"rv_thumbStrokeWidth", (int)PixelUtil.fp2px(2));
        mThumbRadius = AttrUtils.getDimensionFromAttr(attrs,"rv_thumbRadius", 100);
        //轨道属性
        mTrackDrawableWidth =  AttrUtils.getDimensionFromAttr(attrs,"rv_trackDrawableWidth",0);
        mTrackDrawableHeight = AttrUtils.getDimensionFromAttr(attrs,"rv_trackDrawableHeight", 0);
        mTrackDrawable =AttrUtils.getElementFromAttr(attrs,"rv_trackDrawable",null);
        mTrackPressedDrawable =AttrUtils.getElementFromAttr(attrs,"rv_trackPressedDrawable",null);
        mTrackDisabledDrawable = AttrUtils.getElementFromAttr(attrs,"rv_trackDisabledDrawable",null);
        mTrackSelectedDrawable = AttrUtils.getElementFromAttr(attrs,"rv_trackSelectedDrawable",null);
        mTrackCheckedDrawable = AttrUtils.getElementFromAttr(attrs,"rv_trackCheckedDrawable",null);
        mTrackStrokeColor = AttrUtils.getColorFromAttr(attrs,"rv_trackStrokeColor", mColorDefault);
        mTrackStrokePressedColor = AttrUtils.getColorFromAttr(attrs,"rv_trackStrokePressedColor", mTrackStrokeColor);
        mTrackStrokeDisabledColor = AttrUtils.getColorFromAttr(attrs,"rv_trackStrokeDisabledColor", mTrackStrokeColor);
        mTrackStrokeSelectedColor = AttrUtils.getColorFromAttr(attrs,"rv_trackStrokeSelectedColor", mThumbStrokeColor);
        mTrackStrokeCheckedColor = AttrUtils.getColorFromAttr(attrs,"rv_trackStrokeCheckedColor", mColorAccent);
        mTrackStrokeWidth = AttrUtils.getDimensionFromAttr(attrs,"rv_trackStrokeWidth", (int)PixelUtil.fp2px(2));
        mTrackRadius = AttrUtils.getDimensionFromAttr(attrs,"rv_trackRadius", 100);
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(RgbColor.fromArgbInt(Color.WHITE.getValue()));
        mThumbDrawable = mThumbDrawable == null ? shapeElement : mThumbDrawable;
        mThumbPressedDrawable = mTrackPressedDrawable == null ? mThumbDrawable : mThumbPressedDrawable;
        mThumbDisabledDrawable = mThumbDisabledDrawable == null ? mThumbDrawable : mThumbDisabledDrawable;
        mThumbSelectedDrawable = mThumbSelectedDrawable == null ? mThumbDrawable : mThumbSelectedDrawable;

        ShapeElement shapeElement4 = new ShapeElement();
        shapeElement4.setRgbColor(RgbColor.fromArgbInt(Color.WHITE.getValue()));
        mThumbCheckedDrawable = mThumbCheckedDrawable == null ? shapeElement4 : mThumbCheckedDrawable;
        ShapeElement shapeElement2 = new ShapeElement();
        shapeElement2.setRgbColor(RgbColor.fromArgbInt(mColorDefault));
        mTrackDrawable = mTrackDrawable == null ? shapeElement2 : mTrackDrawable;
        mTrackPressedDrawable = mTrackPressedDrawable == null ? mTrackDrawable : mTrackPressedDrawable;
        mTrackDisabledDrawable = mTrackDisabledDrawable == null ? mTrackDrawable : mTrackDisabledDrawable;
        mTrackSelectedDrawable = mTrackSelectedDrawable == null ? mTrackDrawable : mTrackSelectedDrawable;
        ShapeElement shapeElement3 = new ShapeElement();
        shapeElement3.setRgbColor(RgbColor.fromArgbInt(mColorAccent));
        mTrackCheckedDrawable = mTrackCheckedDrawable == null ? shapeElement3 : mTrackCheckedDrawable;
        super.initAttributes(context, attrs);
    }

    @Override
    public void init() {
        super.init();
        setSwitchDrawable();
    }

    /**
     * 设置滑块宽度
     *
     * @param width
     * @return RadiusSwitchDelegate
     */
    public RadiusSwitchDelegate setThumbDrawableWidth(int width) {
        mThumbDrawableWidth = width;
        return this;
    }

    /**
     * 设置滑块高度
     *
     * @param height
     * @return RadiusSwitchDelegate
     */
    public RadiusSwitchDelegate setThumbDrawableHeight(int height) {
        mThumbDrawableHeight = height;
        return this;
    }

    /**
     * 设置滑块默认状态drawable
     *
     * @param drawable
     * @return RadiusSwitchDelegate
     */
    public RadiusSwitchDelegate setThumbDrawable(Element drawable) {
        mThumbDrawable = drawable;
        return this;
    }

    public RadiusSwitchDelegate setThumbDrawable(int resId) {
        return setThumbDrawable(getDrawable(resId));
    }

    /**
     * 设置滑块按下状态drawable
     *
     * @param drawable
     * @return RadiusSwitchDelegate
     */
    public RadiusSwitchDelegate setThumbPressedDrawable(Element drawable) {
        mThumbPressedDrawable = drawable;
        return this;
    }

    public RadiusSwitchDelegate setThumbPressedDrawable(int resId) {
        return setThumbPressedDrawable(getDrawable(resId));
    }

    public RadiusSwitchDelegate setThumbDisabledDrawable(Element drawable) {
        mThumbDisabledDrawable = drawable;
        return this;
    }

    public RadiusSwitchDelegate setThumbDisabledDrawable(int resId) {
        return setThumbDisabledDrawable(getDrawable(resId));
    }

    public RadiusSwitchDelegate setThumbSelectedDrawable(Element thumbSelectedDrawable) {
        mThumbSelectedDrawable = thumbSelectedDrawable;
        return this;
    }

    public RadiusSwitchDelegate setThumbSelectedDrawable(int resId) {
        return setThumbSelectedDrawable(getDrawable(resId));
    }

    /**
     * 设置滑块checked状态drawable
     *
     * @param drawable
     * @return RadiusSwitchDelegate
     */
    public RadiusSwitchDelegate setThumbCheckedDrawable(Element drawable) {
        mThumbCheckedDrawable = drawable;
        return this;
    }

    public RadiusSwitchDelegate setThumbCheckedDrawable(int resId) {
        return setThumbCheckedDrawable(getDrawable(resId));
    }

    /**
     * 设置滑块边框颜色
     *
     * @param color
     * @return RadiusSwitchDelegate
     */
    public RadiusSwitchDelegate setThumbStrokeColor(int color) {
        mThumbStrokeColor = color;
        return this;
    }

    public RadiusSwitchDelegate setThumbStrokePressedColor(int color) {
        mThumbStrokePressedColor = color;
        return this;
    }

    public RadiusSwitchDelegate setThumbStrokeDisabledColor(int color) {
        mThumbStrokeDisabledColor = color;
        return this;
    }

    public RadiusSwitchDelegate setThumbStrokeSelectedColor(int color) {
        mThumbStrokeSelectedColor = color;
        return this;
    }

    /**
     * 设置滑块边框选中状态颜色
     *
     * @param color
     * @return RadiusSwitchDelegate
     */
    public RadiusSwitchDelegate setThumbStrokeCheckedColor(int color) {
        mThumbStrokeCheckedColor = color;
        return this;
    }

    /**
     * 设置滑块边框宽度
     *
     * @param width
     * @return RadiusSwitchDelegate
     */
    public RadiusSwitchDelegate setThumbStrokeWidth(int width) {
        mThumbStrokeWidth = width;
        return this;
    }

    /**
     * 设置边框圆角弧度
     *
     * @param radius
     * @return RadiusSwitchDelegate
     */
    public RadiusSwitchDelegate setThumbRadius(int radius) {
        mThumbRadius = radius;
        return this;
    }

    /**
     * 设置轨道宽度
     *
     * @param width
     * @return RadiusSwitchDelegate
     */
    public RadiusSwitchDelegate setTrackDrawableWidth(int width) {
        mTrackDrawableWidth = width;
        return this;
    }

    /**
     * 设置轨道高度
     *
     * @param height
     * @return RadiusSwitchDelegate
     */
    public RadiusSwitchDelegate setTrackDrawableHeight(int height) {
        mTrackDrawableHeight = height;
        return this;
    }

    /**
     * 设置轨道默认drawable
     *
     * @param drawable
     * @return RadiusSwitchDelegate
     */
    public RadiusSwitchDelegate setTrackDrawable(Element drawable) {
        mTrackDrawable = drawable;
        return this;
    }

    public RadiusSwitchDelegate setTrackDrawable(int resId) {
        return setTrackDrawable(getDrawable(resId));
    }

    public RadiusSwitchDelegate setTrackPressedDrawable(Element drawable) {
        mTrackPressedDrawable = drawable;
        return this;
    }

    public RadiusSwitchDelegate setTrackPressedDrawable(int resId) {
        return setTrackPressedDrawable(getDrawable(resId));
    }

    public RadiusSwitchDelegate setTrackDisabledDrawable(Element drawable) {
        mTrackDisabledDrawable = drawable;
        return this;
    }

    public RadiusSwitchDelegate setTrackDisabledDrawable(int resId) {
        return setTrackDisabledDrawable(getDrawable(resId));
    }

    public RadiusSwitchDelegate setTrackSelectedDrawable(Element drawable) {
        mTrackSelectedDrawable = drawable;
        return this;
    }

    public RadiusSwitchDelegate setTrackSelectedDrawable(int resId) {
        return setTrackSelectedDrawable(getDrawable(resId));
    }

    /**
     * 设置轨道checked状态drawable
     *
     * @param drawable
     * @return RadiusSwitchDelegate
     */
    public RadiusSwitchDelegate setTrackCheckedDrawable(Element drawable) {
        mTrackCheckedDrawable = drawable;
        return this;
    }

    public RadiusSwitchDelegate setTrackCheckedDrawable(int resId) {
        return setTrackCheckedDrawable(getDrawable(resId));
    }

    /**
     * 设置轨道边框颜色
     *
     * @param color
     * @return RadiusSwitchDelegate
     */
    public RadiusSwitchDelegate setTrackStrokeColor(int color) {
        mTrackStrokeColor = color;
        return this;
    }

    public RadiusSwitchDelegate setTrackStrokePressedColor(int color) {
        mTrackStrokePressedColor = color;
        return this;
    }

    public RadiusSwitchDelegate setTrackStrokeDisabledColor(int color) {
        mTrackStrokeDisabledColor = color;
        return this;
    }

    public RadiusSwitchDelegate setTrackStrokeSelectedColor(int color) {
        mTrackStrokeSelectedColor = color;
        return this;
    }

    /**
     * 设置轨道边框checked状态颜色
     *
     * @param color
     * @return RadiusSwitchDelegate
     */
    public RadiusSwitchDelegate setTrackStrokeCheckedColor(int color) {
        mTrackStrokeCheckedColor = color;
        return this;
    }

    /**
     * 设置轨道边框宽度
     *
     * @param width
     * @return RadiusSwitchDelegate
     */
    public RadiusSwitchDelegate setTrackStrokeWidth(int width) {
        mTrackStrokeWidth = width;
        return this;
    }

    /**
     * 设置轨道圆角弧度
     *
     * @param radius
     * @return RadiusSwitchDelegate
     */
    public RadiusSwitchDelegate setTrackRadius(int radius) {
        mTrackRadius = radius;
        return this;
    }

    /**
     * 设置Drawable相关属性
     */
    private void setSwitchDrawable() {
        mSwitch = (Switch) mView;
        mStateThumbDrawable = new StateElement();
        mStateTrackDrawable = new StateElement();
        mStateThumbDrawable.addState(new int[]{mStateChecked}, getStateDrawable(mThumbCheckedDrawable, mThumbRadius, mThumbDrawableWidth, mThumbDrawableHeight, mThumbStrokeWidth, mThumbStrokeCheckedColor));
        mStateThumbDrawable.addState(new int[]{mStateSelected}, getStateDrawable(mThumbSelectedDrawable, mThumbRadius, mThumbDrawableWidth, mThumbDrawableHeight, mThumbStrokeWidth, mThumbStrokeSelectedColor));
        mStateThumbDrawable.addState(new int[]{mStatePressed}, getStateDrawable(mThumbPressedDrawable, mThumbRadius, mThumbDrawableWidth, mThumbDrawableHeight, mThumbStrokeWidth, mThumbStrokePressedColor));
        mStateThumbDrawable.addState(new int[]{mStateDisabled}, getStateDrawable(mThumbDisabledDrawable, mThumbRadius, mThumbDrawableWidth, mThumbDrawableHeight, mThumbStrokeWidth, mThumbStrokeDisabledColor));
        mStateThumbDrawable.addState(new int[]{}, getStateDrawable(mThumbDrawable, mThumbRadius, mThumbDrawableWidth, mThumbDrawableHeight, mThumbStrokeWidth, mThumbStrokeColor));

    //    mTrackDrawableHeight = 0;
        mStateTrackDrawable.addState(new int[]{mStateChecked}, getStateDrawable(mTrackCheckedDrawable, mTrackRadius, mTrackDrawableWidth, mTrackDrawableHeight, mTrackStrokeWidth, mTrackStrokeCheckedColor));
        mStateTrackDrawable.addState(new int[]{mStateSelected}, getStateDrawable(mTrackSelectedDrawable, mTrackRadius, mTrackDrawableWidth, mTrackDrawableHeight, mTrackStrokeWidth, mTrackStrokeSelectedColor));
        mStateTrackDrawable.addState(new int[]{mStatePressed}, getStateDrawable(mTrackPressedDrawable, mTrackRadius, mTrackDrawableWidth, mTrackDrawableHeight, mTrackStrokeWidth, mTrackStrokePressedColor));
        mStateTrackDrawable.addState(new int[]{mStateDisabled}, getStateDrawable(mTrackDisabledDrawable, mTrackRadius, mTrackDrawableWidth, mTrackDrawableHeight, mTrackStrokeWidth, mTrackStrokeDisabledColor));
        mStateTrackDrawable.addState(new int[]{}, getStateDrawable(mTrackDrawable, mTrackRadius, mTrackDrawableWidth, mTrackDrawableHeight, mTrackStrokeWidth, mTrackStrokeColor));

        mSwitch.setThumbElement(mStateThumbDrawable);
        mSwitch.setTrackElement(mStateTrackDrawable);

    }


    protected Element getStateDrawable(Element drawable, float radius, int width, int height, int strokeWidth, int strokeColor) {
        drawable = getStateDrawable(drawable, radius, width, height);
        if (drawable instanceof ShapeElement) {
            ShapeElement gradientDrawable = (ShapeElement) drawable;
            gradientDrawable.setStroke(strokeWidth, RgbColor.fromArgbInt(strokeColor));
        }
        return drawable;
    }
}
